<?php

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Session;

class Index extends Authorized
{
    public function index(){return $this->fetch();}
    public function console()
    {

//	所有 客户（包含代理商）：
        $all_user=Db::table('user')->where('state',1)->count();
        $this->assign('all_user', $all_user);

//	站点数量 按天：
        $day_webInfo=Db::table('webInfo')->where('webtype_id',1)->count();
        $this->assign('day_webInfo', $day_webInfo);

        $year_webInfo=Db::table('webInfo')->where('webtype_id',2)->count();
        $this->assign('year_webInfo', $year_webInfo);

        return $this->fetch();
    }

//    获取客户列表
    public function getList(){
        $get_data=$this->request->get();
        $post_data = $this->request->post();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $typeId = input('typeId');
        $where=[];
        $where['state']=1;
        if (!empty($typeId)){
            if($typeId==1){
                $where['money']=['between',[1000,2000]];
            }
            if($typeId==2){$where['money']=['<=',1000];}
        }


        $count=Db::table('tb_user')->where($where)->count();
        $rows=Db::table('tb_user')->where($where)->order('money')->page($page, $limit)->select();

        return json(['code' =>0,'count' =>$count,'msg' =>'success','data'=>$rows]);
    }


    public function change_pwd()
    {
        $data = $this->request->post();
        if($data){
            $res = Db::table('admin')
                ->where([
                    'id' => Session::get('admin.id'),
                    'password'=>md5($data['oldPassword'])
                ])->find();
            if ($res) {
                $result = Db::table('admin')
                    ->where([
                        'id' => Session::get('admin.id')
                    ])
                    ->setField('password',md5($data['password']));
                if ($result) {
                    return json(['msg' =>'修改成功', 'code' =>0]);
                }else{
                    return json(['msg' =>'修改失败', 'code' =>1]);
                }

            }else{
                return json(['msg' =>'旧密码错误', 'code' =>1]);
            }
        }else{
            return $this->fetch();
        }

    }

    public function config()
    {
        $post_data = $this->request->post();
        if($post_data){

            Db::table('config')->startTrans();
            $id =Db::table('config')->where('id',1)->update($post_data);
            if (false === $id) {
                Db::table('config')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'修改失败','data'=>'']);
            } else {
                Db::table('config')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'修改成功','data'=>'']);
            }
        }

        $article=Db::table('config')->where('id',1)->find();
        $this->assign('article', $article);

        return $this->fetch();
    }


}